package com.study.leetcode;
//https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/
public class LongestSubstringWithoutRepeatingCharacters {
    public int lengthOfLongestSubstring(String s) {
        int ans = 0;
        int begin = 0;
        //访问数组用于记录上一次访问
        int[] visit = new int[128];
        for (int end = 0; end < s.length(); end++) {
            //如果当前窗口有重复则将当前窗口移动
            if (visit[s.charAt(end)] != 0) {
                //移动到上一次访问下标加一的位置
                begin = Math.max(begin, visit[s.charAt(end)]);
            }
            //记录要移动的下标
            visit[s.charAt(end)] = end + 1;
            //当前窗口的长度与之前最长的字串经行比较
            ans = Math.max(ans, end - begin + 1);
        }
        return ans;
    }
}
